using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Camera_MoveByMouse : MonoBehaviour
{
    [SerializeField] private GameObject player;
    [Range(0f, 1f)]
    [SerializeField] private float mouseRateX = 0.8f;
    [Range (0f, 1f)]
    [SerializeField] private float mouseRateY = 0.8f;
    private float mousePosX = 0f;
    private float mousePosY = 0f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        TrackPlayerAndMouse();
        if (Input.GetKeyDown(KeyCode.E))
            Debug.Log(Input.mousePosition);
    }

    private Vector3 TrackMouse()
    {
        mousePosX = Mathf.Clamp(Input.mousePosition.x, 0f, Screen.width);
        mousePosY = Mathf.Clamp(Input.mousePosition.y, 0f, Screen.height);
        return new Vector3(mouseRateX * (mousePosX - Screen.width / 2f) * (Camera.main.orthographicSize * 2.0f / Screen.width), mouseRateY * (mousePosY - Screen.height / 2f) * (Camera.main.orthographicSize * 2.0f / Screen.width));
    }

    private void TrackPlayerAndMouse()
    {
        transform.position = new Vector3(player.transform.position.x, player.transform.position.y, transform.position.z) + TrackMouse();
    }
}
